package com.gongpingjialoan.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gongpingjialoan.R;

/**
 * Created by Administrator on 2016/8/3.
 */
public class ColorSelectPop {
    Context context;

    public PopupWindow pop;

    View contentV;


    View bgV;

    TextView titleT;

    OnCheckResult onCheckResult;

    static ColorSelectPop colorSelectPop;

    public ColorSelectPop(Context context) {
        this.context = context;
        contentV = LayoutInflater.from(context).inflate(
                R.layout.color_select_pop, null);
        pop = new PopupWindow(contentV, ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT, true);
        // 需要设置一下此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);
        pop.setAnimationStyle(R.style.PopupAnimation);
        initView();
    }

    public static ColorSelectPop getInstance(Context context) {
        if (colorSelectPop == null) {
            colorSelectPop = new ColorSelectPop(context);
        }
        return colorSelectPop;
    }

    public void initView() {
        titleT = (TextView) contentV.findViewById(R.id.title);
        titleT.setText("车辆颜色");
        bgV = contentV.findViewById(R.id.bg);
        bgV.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                pop.dismiss();
            }
        });

        LinearLayout layout = (LinearLayout) contentV.findViewById(R.id.layout);
        for (int i = 1; i < layout.getChildCount(); i++) {
            View childV = layout.getChildAt(i);
            final TextView text = (TextView) childV.findViewWithTag("text" + i   );
            childV.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (onCheckResult != null && text != null) {
                        onCheckResult.result(text.getText().toString());
                    }
                    pop.dismiss();
                }
            });
        }
    }

    public void show(View v) {
        pop.showAtLocation(v, Gravity.TOP, 0, 0);
    }

    public OnCheckResult getOnCheckResult() {
        return onCheckResult;
    }

    public void setOnCheckResult(OnCheckResult onCheckResult) {
        this.onCheckResult = onCheckResult;
    }


    public interface OnCheckResult {
        void result(String color);
    }

}